#!/usr/bin/env sh

# This file is meant to be executed inside the Vagrant development VM

# Prevent dpkg-reconfigure interactive sessions
export DEBIAN_FRONTEND=noninteractive


### BEGIN SAMPLE PCAP DATA ###
apt-get update
apt-get install -y libpcap-dev tshark
curl -s 'http://www.snaketrap.co.uk/pcaps/hbot.pcap' -o /vagrant/seed/hbot.pcap
### END SAMPLE PCAP DATA ###


### BEGIN OPENLDAP ###
# slapd doesn't seem to work right unless you configure it via debconf
# The password for all users is opensoc
debconf-set-selections /vagrant/seed/slapd.seed
apt-get update
apt-get -y install slapd ldap-utils

cd /vagrant/seed/ldap
ldapmodify -Y EXTERNAL -H ldapi:/// -f logging.ldif
ldapmodify -Y EXTERNAL -H ldapi:/// -f config.ldif
ldapadd -D cn=admin,cn=config -w "opensoc" -H ldapi:/// -f memberof_add.ldif
ldapadd -D cn=admin,cn=config -w "opensoc" -H ldapi:/// -f memberof_config.ldif

# SEED LDAP
ldapadd -h localhost -p 389 -D "cn=admin,dc=opensoc,dc=dev" -w opensoc -f content.ldif
cd -
### END OPENLDAP ###


### BEGIN ELASTICSEARCH ###
# TODO(ram): Upgrade ElasticSearch to 1.3.0
apt-get -y install openjdk-7-jre-headless
wget --quiet https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.deb
dpkg -i elasticsearch-1.1.1.deb
rm -rf elasticsearch-1.1.1.deb
cd /usr/share/elasticsearch
bin/plugin -install mobz/elasticsearch-head
cd -
update-rc.d elasticsearch defaults 95 10
service elasticsearch restart

# SEED ELASTICSEARCH
cd /vagrant
su vagrant -c "script/es_gen.js"
su vagrant -c "script/es_seed"
cd -
### END ELASTICSEARCH ###

### NODE SETUP ###
apt-get update
apt-get install -y nodejs npm git

ln -s /usr/bin/nodejs /usr/bin/node
npm install -g nodemon
npm install -g grunt-cli

cd /vagrant/kibana
rm -rf node_modules dist
su vagrant -c "npm install"
cd -

cd /vagrant
rm -rf node_modules
su vagrant -c "npm install"
su vagrant -c  "echo 'export NODE_ENV=vagrant' >> /home/vagrant/.bashrc"
su vagrant -c "ln -s /vagrant/.nodemonignore ~/.nodemonignore"
cd -
### END NODE SETUP ###
